package com.zhs.gccs.view.gt;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.model.CheckBoxModel;
import com.zhs.gccs.view.BaseView;

import java.util.ArrayList;
import java.util.List;

public class ProblemSingleSelectView extends BaseView {
    private TextView tv_cancel;
    private TextView tv_confirm;
    private View ll_cb_all;//
    private CheckBox cb_all_selected;
    private RecyclerView rv_items;
    private List<CheckBoxModel> dataList;
    private UserItemAdapter adapter;

    public ProblemSingleSelectView(@NonNull Context context) {
        super(context);
    }

    public ProblemSingleSelectView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ProblemSingleSelectView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_single_problem_select,
                this, true);
        ll_cb_all = view.findViewById(R.id.ll_cb_all);
        cb_all_selected = view.findViewById(R.id.cb_all_selected);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_confirm = view.findViewById(R.id.tv_confirm);
        rv_items = view.findViewById(R.id.rv_items);
        tv_cancel.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                Stream.of(dataList).forEach(item->item.setSelected(false));
                cb_all_selected.setChecked(false);
                adapter.notifyDataSetChanged();
//                popupWindow.dismiss();
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
        tv_confirm.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                if (dataChangeListener != null) dataChangeListener.dismis();
            }
        });

        this.dataList = new ArrayList<>();
        rv_items.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        adapter = new UserItemAdapter(context, dataList);
        rv_items.setAdapter(adapter);
        adapter.setOperationListener(new UserItemAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                Stream.of(dataList).forEach(item->item.setSelected(false));
                CheckBoxModel checkBoxModel = dataList.get(position);
                checkBoxModel.setSelected(true);//(!checkBoxModel.isSelected());
                adapter.notifyDataSetChanged();
//                long count = Stream.of(dataList).filter(i -> i.isSelected()).count();
//                if(count!= dataList.size()){
//                    cb_all_selected.setChecked(false);
//                }else {
//                    cb_all_selected.setChecked(true);
//                }
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
        ll_cb_all.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                if(cb_all_selected.isChecked()){
                    cb_all_selected.setChecked(false);
                    Stream.of(dataList).forEach(i -> i.setSelected(false));
                }else {
                    cb_all_selected.setChecked(true);
                    Stream.of(dataList).forEach(i -> i.setSelected(true));
                }
                adapter.notifyDataSetChanged();
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
    }


    public void initData() {
        this.dataList.clear();;
        this.dataList.add(new CheckBoxModel("合格", "1", false));
        this.dataList.add(new CheckBoxModel("不合格", "0", false));
        adapter.notifyDataSetChanged();
    }

    public void setDataChangeListener(DataChangeListener dataChangeListener) {
        this.dataChangeListener = dataChangeListener;
    }

    private DataChangeListener dataChangeListener;

    public CheckBoxModel getSelected() {
        return Stream.of(dataList).filter(i -> i.isSelected()).findFirstOrElse(null);
    }

    public List<CheckBoxModel> getSelectedItems() {
        return Stream.of(dataList).filter(i -> i.isSelected()).toList();
    }

    public void setSelectedYear(String year) {
        if(TextUtils.isEmpty(year)){
            return;
        }else {
            Stream.of(dataList).forEach(i -> {
                if(i.getValue().equals(year)){
                    i.setSelected(true);
                }
            });
        }
        adapter.notifyDataSetChanged();
    }




    public interface DataChangeListener{
        void  dataChange();
        void dismis();
    }
}
